using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 眩晕Buff - 使目标无法行���
/// </summary>
public class StunBuff : BuffBase
{
    private bool m_OriginalCanMove;
    private bool m_OriginalCanAttack;
    private bool m_OriginalCanCast;

    public StunBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始状���
        m_OriginalCanMove = Target.MoveComponent != null;
        m_OriginalCanAttack = Target.AttackComponent != null;
        m_OriginalCanCast = Target.SkillAttackIndicators != null;
        
        // 禁用移动、攻击和技���
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = false;
        }
        
        if (Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = false;
        }
        
        if (Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = false;
        }
        
        // 播放眩晕特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("stunned", true);
        }
        
        Logger.Log($"{Target.name} 被眩晕了，无法行���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 眩晕期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("StunBuff", $"{Target.name} 眩晕效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始状���
        if (m_OriginalCanMove && Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = true;
        }
        
        if (m_OriginalCanAttack && Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = true;
        }
        
        if (m_OriginalCanCast && Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = true;
        }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("stunned", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Stun"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 眩晕效果不能叠加，只能刷新持续时���
    }
}
